package org.dromara.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.iot.domain.IotElevatorFault;
import org.dromara.iot.domain.bo.IotElevatorFaultBo;
import org.dromara.iot.domain.vo.IotElevatorFaultVo;
import org.dromara.iot.mapper.IotElevatorFaultMapper;
import org.dromara.iot.service.IIotElevatorFaultService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 电梯故障Service业务层处理
 *
 * @author lsl
 * @date 2024-06-15
 */
@RequiredArgsConstructor
@Service
public class IotElevatorFaultServiceImpl implements IIotElevatorFaultService {

    private final IotElevatorFaultMapper baseMapper;

    /**
     * 查询电梯故障
     */
    @Override
    public IotElevatorFaultVo queryById(String faultId) {
        return baseMapper.selectVoById(faultId);
    }

    /**
     * 查询电梯故障列表
     */
    @Override
    public TableDataInfo<IotElevatorFaultVo> queryPageList(IotElevatorFaultBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IotElevatorFault> lqw = buildQueryWrapper(bo);
        Page<IotElevatorFaultVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询电梯故障列表
     */
    @Override
    public List<IotElevatorFaultVo> queryList(IotElevatorFaultBo bo) {
        LambdaQueryWrapper<IotElevatorFault> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IotElevatorFault> buildQueryWrapper(IotElevatorFaultBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IotElevatorFault> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getElevatorId() != null, IotElevatorFault::getElevatorId, bo.getElevatorId());
        lqw.like(StringUtils.isNotBlank(bo.getElevatorName()), IotElevatorFault::getElevatorName, bo.getElevatorName());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultCode()), IotElevatorFault::getFaultCode, bo.getFaultCode());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairOrder()), IotElevatorFault::getRepairOrder, bo.getRepairOrder());
        lqw.eq(StringUtils.isNotBlank(bo.getDispatchPeople()), IotElevatorFault::getDispatchPeople, bo.getDispatchPeople());
        lqw.eq(bo.getDispatchDate() != null, IotElevatorFault::getDispatchDate, bo.getDispatchDate());
        lqw.eq(StringUtils.isNotBlank(bo.getReportType()), IotElevatorFault::getReportType, bo.getReportType());
        lqw.eq(bo.getReportTime() != null, IotElevatorFault::getReportTime, bo.getReportTime());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultType()), IotElevatorFault::getFaultType, bo.getFaultType());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultMessage()), IotElevatorFault::getFaultMessage, bo.getFaultMessage());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultImage()), IotElevatorFault::getFaultImage, bo.getFaultImage());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultFloor()), IotElevatorFault::getFaultFloor, bo.getFaultFloor());
        lqw.eq(bo.getRemoveTime() != null, IotElevatorFault::getRemoveTime, bo.getRemoveTime());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultDuration()), IotElevatorFault::getFaultDuration, bo.getFaultDuration());
        lqw.eq(StringUtils.isNotBlank(bo.getDisposeState()), IotElevatorFault::getDisposeState, bo.getDisposeState());
        lqw.eq(StringUtils.isNotBlank(bo.getDisposeMan()), IotElevatorFault::getDisposeMan, bo.getDisposeMan());
        lqw.eq(bo.getDisposeDate() != null, IotElevatorFault::getDisposeDate, bo.getDisposeDate());
        lqw.eq(StringUtils.isNotBlank(bo.getDisposeOpin()), IotElevatorFault::getDisposeOpin, bo.getDisposeOpin());
        lqw.eq(StringUtils.isNotBlank(bo.getIsOverhaul()), IotElevatorFault::getIsOverhaul, bo.getIsOverhaul());
        lqw.orderByDesc(IotElevatorFault::getReportTime);
        return lqw;
    }

    /**
     * 新增电梯故障
     */
    @Override
    public Boolean insertByBo(IotElevatorFaultBo bo) {
        IotElevatorFault add = MapstructUtils.convert(bo, IotElevatorFault.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFaultId(add.getFaultId());
        }
        return flag;
    }

    /**
     * 修改电梯故障
     */
    @Override
    public Boolean updateByBo(IotElevatorFaultBo bo) {
        IotElevatorFault update = MapstructUtils.convert(bo, IotElevatorFault.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IotElevatorFault entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除电梯故障
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据故障号查询故障信息
     *
     * @param faultCode 故障号
     * @return 故障信息
     */
    @Override
    public IotElevatorFaultVo queryByFaultCode(String faultCode) {
        LambdaQueryWrapper<IotElevatorFault> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(IotElevatorFault::getFaultCode, faultCode);
        return baseMapper.selectVoOne(wrapper);
    }
}
